package com.hzhr.iot.login.sdk.service;

import com.hzhr.iot.core.constant.Common_ErrorsEnum;
import com.hzhr.iot.core.dto.ResponseExceptionDto;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrHttpUtil;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.login.sdk.constant.Login_ErrorsEnum;
import com.hzhr.iot.login.sdk.model.token.TokenModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

@Service
public class Login_TokenService {
    @Autowired
    RestTemplate restTemplate;

    /**
     * 获取用户的登录令牌 <br/><br/>
     * 简介&nbsp;:&nbsp;获取用户的登录令牌 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;认证用户 <br/>
     * 2.&nbsp;创建令牌 <br/>
     * 3.&nbsp;如果用户存在当前卡片，直接返回；否则，创建新用户卡片并更新用户当前令牌 <br/>
     * 4.&nbsp;如果用户没有rsToken，则创建rsToken并返回；否则，直接返回 <br/>
     * 5.&nbsp;返回创建的令牌 <br/>
     * @param header <br/><br/>
     * @param query <br/><br/>
     * @throws ServiceException <br/>
     * TOKENSERVICE_GET_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 认证用户<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 用户不存在或密码不匹配<br/>
     * @see TokenModel result <br/>
     */ 
    public TokenModel get(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(null, headers);

        Map<String, Object> uriVariables = new HashMap<>();

        try {
            ResponseEntity<TokenModel> result = restTemplate.exchange("http://login-service/tokens?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.GET, httpEntity, TokenModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(Login_ErrorsEnum.getEnumByName("TOKENSERVICE_GET_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(Login_ErrorsEnum.getEnumByName("TOKENSERVICE_GET_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
}

